package com.only4play.portal.web.handler;

import com.only4play.portal.common.api.PortalConstants;
import com.only4play.portal.common.session.PortalSessionManager;
import com.only4play.portal.cp.api.CpService;
import com.only4play.portal.cp.bean.message.jsonmessage.CpJsonMessage;
import com.only4play.portal.cp.bean.message.jsonmessage.CpJsonOutMessage;
import com.only4play.portal.cp.bean.message.jsonmessage.CpJsonOutTextMessage;
import com.only4play.portal.cp.message.CpMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file SubscribeHandler
 * @brief SubscribeHandler
 * @details SubscribeHandler
 * @date 2024-06-11
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-06-11               liyuncong          Created
 */

@Slf4j
@Component
public class SubscribeHandler implements CpMessageHandler<CpJsonOutMessage, CpJsonMessage> {

    private final static String CREATE_USER = "create_user";

    @Override
    public CpJsonOutMessage handle(
        CpJsonMessage cpJsonMessage,
        Map<String, Object> context,
        CpService portalCpService,
        PortalSessionManager sessionManager
    ) {
        log.info("SubscribeHandler: this message is coming here now");
        String changeType = cpJsonMessage.getChangeType();
        String event = cpJsonMessage.getEvent();
        if (CREATE_USER.equals(changeType) || PortalConstants.EventType.SUBSCRIBE.equals(event)) {
            // do somethings
            CpJsonOutTextMessage cpJsonOutMessage = new CpJsonOutTextMessage();
            cpJsonOutMessage.setCreatedAt(Instant.now().toEpochMilli());
            cpJsonOutMessage.setContent("fuck u tony");
            return cpJsonOutMessage;
        }
        return null;
    }
}
